// pages/notice_detail_task/notice_detail_task.js
const request = require('../../utils/promise.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    msgType: 0,
    messageId: 0,
    taskId: 0,
    messageInfo: {},
    inputValue: 0,
    currUserId: 0,
    isProcessed: true, // 是否已处理（已处理的消息，则不用在显示相关处理按钮）
    managerUserId: 0,
    managerUsername: '',
    projectId: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 初始值
    this.setData({
      msgType: options.type,
      messageId: options.messageId,
      taskId: options.taskId? options.taskId: 0,
      currUserId: wx.getStorageSync('userId'),
      projectId: options.projectId? options.projectId: null
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMsgInfo();  // 获取消息数据
  },

  /**
   * 页面被用户分享时执行
   */
  onShareAppMessage() {
    return {
      title: '消息详情'
    }
  },

  /**
   * 分享到朋友圈
   */
  onShareTimeline() {
    return {
      title: '消息详情'
    }
  },

   /**
   * 进入任务详情
   */
  gotoTaskDetail(e) {
    wx.navigateTo({
      url: '/pages/task_detail/task_detail?id='+ this.data.taskId,
    })
  },

  /**
   * 获取消息数据, 并设置为已读
   */
  getMsgInfo() {
    request.GET('/message/messageInfo', {id: this.data.messageId}).then(res => {
      // console.log(res)
      if (res.data.resCode==200) {
        this.setData({
          messageInfo: res.data.messageInfo
        })
      } else {
        wx.showToast({
          title: '获取数据异常',
          icon: 'error',
          duration: 1500
        })
      }
    });

    // 获取这个批次的消息，是否已处理，以及处理人
    request.GET('/message/getMsgLastUpdateManager', {messageId: this.data.messageId}).then(res => {
      if (res.statusCode==200 && res.data!="") {
        this.setData({
          managerUserId: res.data.id,
          managerUsername: res.data.username,
          isProcessed: true
        });
      } else {
        this.setData({
          isProcessed:false
        });
      }
    });
  },

  bindKeyInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
  },

  /**
   * 忽略则返回上一级目录
   */
  gotoBack(e) {
    wx.navigateBack({
      delta: 1,
    })
  },

  /**
   * 积分奖励
   */
  clickAgreed(e) {
    // 同意时，必须输入奖励积分
    if (this.data.inputValue=='') {
      wx.showModal({
        title: '温馨提示',
        content: '请输入奖励积分',
        showCancel: false
      })
      return false
    }

    // 单人任务，完成的奖励积分，不能超过任务的积分
    let taskType = this.data.messageInfo.taskType;
    let taskScore = this.data.messageInfo.taskScore;
    let completeScore = this.data.inputValue;
    if (taskType == 1 && completeScore>taskScore) {
      wx.showModal({
        title: '温馨提示',
        content: '属于单人任务，该任务的奖励积分上限为：'+taskScore,
        showCancel: false
      })
      return false
    }

    let params = {
      completeScore: completeScore,
      updatedBy: this.data.currUserId,
      taskId: this.data.messageInfo.taskId,
      userId: this.data.messageInfo.userId,
      messageId: this.data.messageInfo.id,
      taskType: taskType,
      taskScore: taskScore
    }
    request.POST('/message/agreedCompleteTask', params).then(res => {
      if (res.data.resCode==200) {
        setTimeout(function() {
          wx.navigateBack({
            delta: 1,
          })
        }, 500)
      } else if(res.data.resCode==1000){
        setTimeout(function() {
          wx.showModal({
            title: '温馨提示',
            content: '相关任务负责人已审批 或 由于属于多人任务，您分配的积分超出上限',
            showCancel: false
          })
        }, 1000)
        // 刷新页面
        this.getMsgInfo();
      } else {
        wx.showToast({
          title: '保存异常失败',
          icon: 'error',
          duration: 1000
        })
      }
    })
  },

  /**
   * 点击查看邀请的项目详情
   */
  clickViewProjectDetail(e) {
    wx.navigateTo({
      url: '/pages/project_list_detail/project_list_detail?id='+ e.currentTarget.dataset.projectid 
      +'&userVip='+ this.data.messageInfo['toUserVip'],
    })
  },

  /**
   * 点击同意/拒绝项目邀请
   */
  clickHandleProjectInvite(e) {
    wx.showToast({
      title: '操作中...',
      icon: 'loading',
      duration: 30000,
      mask: true
    })

    const param  = {
      id: this.data.messageId,
      projectId: this.data.projectId, 
      userId: wx.getStorageSync('userId'),
      status: e.currentTarget.dataset.status
    }
    request.GET('/message/projectInvited/save', param).then(res => {
        if (res.data.resCode==200) {
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 1000
          })

          setTimeout(function() {
            wx.navigateBack({
              delta: 1,
            })
          }, 1000)
        } else {
          wx.showToast({
            title: '操作异常',
            icon: 'error',
            duration: 1500
          })
        }
    })
  },

})